package com.jlkf.service.impl;

import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.jlkf.dao.TblOrderMapper;
import com.jlkf.dao.TblUserMapper;
import com.jlkf.dao.TblUserPurseInfoMapper;
import com.jlkf.dao.TblVipInfoMapper;
import com.jlkf.pojo.TblOrder;
import com.jlkf.pojo.TblUser;
import com.jlkf.pojo.TblUserPurseInfo;
import com.jlkf.pojo.TblVipInfo;
import com.jlkf.service.VipInfoService;
import com.platform.server.response.response.pojo.ReturnData;
import com.platform.server.util.CfbpayDemoUtils;
import com.platform.server.util.MD5Util;
import com.platform.server.util.RSAUtil;
import com.platform.server.util.StringTools;
import com.platform.server.util.WxpayDemoUtils;

@Service
public class VipInfoServiceImpl implements VipInfoService {

	@Autowired
	private TblVipInfoMapper tblVipInfoMapper;
	
	@Autowired
	private TblUserMapper tblUserMapper;
	
	@Autowired
	private TblOrderMapper tblOrderMapper;
	
	@Autowired
	private TblUserPurseInfoMapper tblUserPurseInfoMapper;
	
	/**
     * 查看vip列表
     */
	@Override
	public ReturnData selectVipList() {
		List<TblVipInfo> lists = tblVipInfoMapper.selectVipList();
		return new ReturnData(lists,200,"成功");
	}

	 /**
     * APP开通vip
	 * @throws Exception 
     */
	@Override
	public ReturnData APPInsertVip(Long userid,Integer vipid, String payMethod, String body, String subject, String payMoney,String ip,String password) throws Exception {
		if(userid == null || vipid == null || StringTools.isEmpty(payMoney) || StringTools.isEmpty(body) || StringTools.isEmpty(subject)
				|| StringTools.isEmpty(payMoney) || StringTools.isEmpty(ip)){
			return new ReturnData(12,"参数有误");
		}
		ReturnData ReturnData = new ReturnData();
		String orderid = WxpayDemoUtils.getRandomString(16);
		if("1".equals(payMethod)){
			ReturnData = WxpayDemoUtils.wxPay(body, orderid, payMoney,ip,userid + "," + vipid + "," + payMoney + "," + orderid,1);
		}else if("2".equals(payMethod)){
			ReturnData = CfbpayDemoUtils.returnAlipayPayParam(body, subject, orderid, payMoney,userid + "," + vipid + "," + payMoney + "," + orderid,1);
		}else if("3".equals(payMethod)){
			TblUser user = tblUserMapper.selectByPrimaryKey(userid);
			if(user == null){
				return new ReturnData(15,"未找到该用户信息");
			}
			String pay_password = user.getgUserPayPassword();		//用户钱包密码
			if(StringTools.isEmpty(pay_password)){
				return new ReturnData(20,"请先设置支付密码");
			}
			Double money = user.getgUserMoney();		//用户钱包金额
			if(money < Double.parseDouble(payMoney)){
				return new ReturnData(30,"钱包金额不足，请先充值");
			}
			//RSA解密
			String payWord = RSAUtil.privateDecrypt(password, RSAUtil.getPrivateKey(RSAUtil.privateKey));
			if(!MD5Util.getMD5(payWord).equals(pay_password)){
				return new ReturnData(40,"密码错误");
			}
			//减少用户钱包金额
			TblUser tuser = new TblUser();
			tuser.setgId(userid);
			tuser.setgUserMoney(money - Double.parseDouble(payMoney));
			tuser.setgUserIsVip(vipid);
			tuser.setgUserPayVipTime(new Date());
			int flag1 = tblUserMapper.updateByPrimaryKeySelective(tuser);		
			//添加会员订单
			TblOrder order = new TblOrder();
			order.setgOrderNo(orderid);
			order.setgCreatetime(new Date());
			order.setgOrderTotalMoney(Double.parseDouble(payMoney));
			order.setgOrderPayMoney(Double.parseDouble(payMoney));
			order.setgOrderStatus(1);
			order.setgVersion(3);
			order.setgOrderType(2);
			order.setgUserId(userid);
			order.setgGoodsName("开通会员");
			order.setgGoodsId(vipid.toString()); //vipId
			order.setgPayTime(new Date());  //购买时间
			//增加余额明细
			TblUserPurseInfo purseInfo = new TblUserPurseInfo();
			purseInfo.setgContent("购买会员成功");
			purseInfo.setgCreatetime(new Date());
			purseInfo.setgMoney(Double.parseDouble(payMoney));
			purseInfo.setgStatus(2);
			purseInfo.setgTitle("购买会员");
			purseInfo.setgType(1);
			purseInfo.setgBalance(money - Double.parseDouble(payMoney));
			purseInfo.setgUserid(userid.longValue());
			tblUserPurseInfoMapper.insertSelective(purseInfo);
			
			TblVipInfo info = tblVipInfoMapper.selectByPrimaryKey(vipid.toString());
			//是否为续期(否：在当前时间追加会员期限   是：在有效期追加会员期限)
			TblOrder tblOrder = tblOrderMapper.getVipInfo(userid);
			Date validity = new Date();
			if(tblOrder!=null){
				if(validity.getTime()<=tblOrder.getgValidityTime().getTime()){
					validity = tblOrder.getgValidityTime();
				}	
			}
			Calendar calendar = new GregorianCalendar(); 
		    calendar.setTime(validity); 
		   
			if(info.getgVipName().contains("月度")){
				calendar.add(calendar.MONTH, 1);
			}else if(info.getgVipName().contains("季度")){
				calendar.add(calendar.MONTH, 3);
			}else if(info.getgVipName().contains("年度")){
				calendar.add(calendar.YEAR, 1);//把日期往后增加一年
			}
			order.setgValidityTime(calendar.getTime());
			int flag = tblOrderMapper.insertSelective(order);
			Map<String,Object> map = new HashMap<>();
			map.put("payMethod", payMethod);
			map.put("payMoney", payMoney);
			map.put("time", new Date());
			if(flag > 0){
				return new ReturnData(map,200,"成功");
			}
		}
		return ReturnData;
	}

	@Override
	public ReturnData isVIP(Long userId) {
		// TODO Auto-generated method stub
		TblOrder order = tblOrderMapper.getVipInfo(userId);
		Date validity = new Date();
		if(order!=null){
			if(validity.getTime()<=order.getgValidityTime().getTime()){
				return new ReturnData(1,200,"是会员");
			}else{
				return new ReturnData(0,200,"不是会员");
			}	
		}
		return new ReturnData(0,200,"不是会员");
	}

	/**
	 * 后台修改vip信息
	 */
	@Override
	public int update(TblVipInfo vipInfo) {
		// TODO Auto-generated method stub
		return tblVipInfoMapper.updateByPrimaryKeySelective(vipInfo);
	}

	/**
	 * 查看详情
	 */
	@Override
	public TblVipInfo getDetail(Long gId) {
		// TODO Auto-generated method stub
		return tblVipInfoMapper.selectByPrimaryKey(gId.toString());
	}
	
	/**
     * 查看vip列表
     */
	@Override
	public List<TblVipInfo> getVipList() {
		return tblVipInfoMapper.selectVipList();
	}

	@Override
	public ReturnData IosInsertVip(Long userid, Integer vipid, String payMoney) {
		// TODO Auto-generated method stub
		try{
			//修改用户信息
			TblUser tuser = new TblUser();
			tuser.setgId(userid);
			tuser.setgUserIsVip(vipid);
			tuser.setgUserPayVipTime(new Date());
			tblUserMapper.updateByPrimaryKeySelective(tuser);		
			//添加会员订单
			String orderid = WxpayDemoUtils.getRandomString(16);
			TblOrder order = new TblOrder();
			order.setgOrderNo(orderid);
			order.setgCreatetime(new Date());
			order.setgOrderTotalMoney(Double.parseDouble(payMoney));
			order.setgOrderPayMoney(Double.parseDouble(payMoney));
			order.setgOrderStatus(1);
			order.setgVersion(3);
			order.setgOrderType(2);
			order.setgUserId(userid);
			order.setgGoodsName("开通会员");
			order.setgGoodsId(vipid.toString()); //vipId
			order.setgPayTime(new Date());  //购买时间
			TblVipInfo info = tblVipInfoMapper.selectByPrimaryKey(vipid.toString());
			//是否为续期(否：在当前时间追加会员期限   是：在有效期追加会员期限)
			TblOrder tblOrder = tblOrderMapper.getVipInfo(userid);
			Date validity = new Date();
			if(tblOrder!=null){
				if(validity.getTime()<=tblOrder.getgValidityTime().getTime()){
					validity = tblOrder.getgValidityTime();
				}	
			}
			Calendar calendar = new GregorianCalendar(); 
		    calendar.setTime(validity); 
		   
			if(info.getgVipName().contains("月度")){
				calendar.add(calendar.MONTH, 1);
			}else if(info.getgVipName().contains("季度")){
				calendar.add(calendar.MONTH, 3);
			}else if(info.getgVipName().contains("年度")){
				calendar.add(calendar.YEAR, 1);//把日期往后增加一年
			}
			order.setgValidityTime(calendar.getTime());
			tblOrderMapper.insertSelective(order);
			return new ReturnData(200,"开通成功");
		}catch (Exception e) {
			e.printStackTrace();
			return new ReturnData(500,"系统错误");
		}
		
	}

}
